#include "UI.h"

const string UI::MESSAGE_EXIT = "See You Again";

UI* UI::theOnlyUI = NULL;

UI::UI(void){
	logic = Logic::getInstance();
}

UI::~UI(void)
{
}

UI* UI::getInstance()
{
	if(!theOnlyUI){
		theOnlyUI = new UI();
	} 
	return theOnlyUI;
}

void UI::initialize(string fileName){

	logic->setFileName(fileName);
	int fileFlag = logic->loadFromFile();
	string message;

	if(fileFlag == 1){
		message = CommonConstant::WELCOME_MESSAGE_LOADED_EXISTING_FILE;
	} else if(fileFlag == 0){
		message = CommonConstant::WELCOME_MESSAGE_NEW_FILE;
	} else{
		message = CommonConstant::WELCOME_MESSAGE_FILE_ERROR;
	}

	show_to_user(message);
}

void UI::processing(){
	while(_feedback != CommonConstant::MESSAGE_EXIT){
		string userCommand;
		prompt_message(userCommand);
		_feedback = logic->executeCommand(userCommand);
	    show_to_user(_feedback);
		write_to_file();
	}
}

Logic::COMMAND_TYPE UI::getCommandType(){
	return logic->getCommandType();
}

void UI::show_to_user(string text){ 
	cout << text << endl;
}

void UI::prompt_message(string &user_command){
	cout << "command: ";
	getline(cin,user_command);
}

void UI::write_to_file(){
    logic->writeToFile();
}

void UI::memoryRelease(){
	logic->memoryRelease();
}